package cc.multivision.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cc.multivision.impl.LoginDaoImpl;
import cc.multivision.model.Login;

@Controller
@RequestMapping
public class LoginController {

	@Autowired
	private Login login;
	
	@Autowired
	@Qualifier("logindao")
	private LoginDaoImpl logindaoimpl;
	
	@RequestMapping()
	public String login(ModelMap model) {
		model.addAttribute("login", login);
		return "index";
	}

	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public String indexPage(@Valid Login test, BindingResult result,
			HttpSession session) {
		if (result.hasErrors()) {
			return "index";
		}else{
			if(logindaoimpl.LoginTest(test)){
				session.setAttribute("user", test.getUsr());
				return "welcome";
			}
			return "index";
			
		}
	}
}